#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "proto.h"
#include "video.h"

#define WinColorA 2
#define WinColorB 3
#define WinColorC 4

#define BackGroundColor  0
int myflag=0;
/* A rough outline of the functino to determine on which window the user has clicked */
int GetWinByXY(int x, int y)
{
	int i, ret;
	ret = -1;
	for(i = NumOfWin - 1; i >= 0; i--)
	{
		if (x >= WinRegister[i].x 	&&	 x < WinRegister[i].x + WinRegister[i].Width &&
		    y >= WinRegister[i].y	&&	 y < WinRegister[i].y + WinRegister[i].Height)
		{
			ret = i;
			break;
		}
	}
	return ret;
}

int RegWin(int x, int y, int Height, int Width, int Type, int Father,int HaveLabel, char Label[])
{
	WinRegister[NumOfWin].ID = NumOfWin;
	WinRegister[NumOfWin].x = x;
	WinRegister[NumOfWin].y = y;
	WinRegister[NumOfWin].Height = Height;
	WinRegister[NumOfWin].Width = Width;	
	WinRegister[NumOfWin].Type = Type;
	WinRegister[NumOfWin].Father = Father;
	WinRegister[NumOfWin].Visible=1;
	WinRegister[NumOfWin].Alive=1;
	WinRegister[NumOfWin].HaveLabel=0;
	strcpy(WinRegister[NumOfWin].Label,Label);
	WinRegister[NumOfWin].Maximized=0;
	NumOfWin++;
	return(NumOfWin - 1);
}

/*void DelWin(int index)
{
	WinRegister[MyWin].Visible=0;
	WinRegister[MyClose].Visible=0;
	WinRegister[MyMaximize].Visible=0;
	WinRegister[MyMinimize].Visible=0;
	EraseWin(MyWin);
	EraseWin(MyClose);
	EraseWin(MyMaximize);
	EraseWin(MyMinimize);
}
*/
void EraseWin(int Index)
{
	int i, temp;
	switch(WinRegister[Index].Type)
	{
		case 0:	
			
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , BackGroundColor);
				vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y +6, BackGroundColor);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , BackGroundColor);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , BackGroundColor);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, BackGroundColor);
			}
			
			for(i = 0; i < NumOfWin; i++) 
				if (WinRegister[i].Father == Index) EraseWin(i);

		break;

		case 1:
			temp = WinRegister[Index].Father;
			vesa_paint_letter('X',WinRegister[temp].x+1,WinRegister[temp].y+1,BackGroundColor);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[temp].x + i, WinRegister[temp].y , BackGroundColor);
	  			vesa_draw_point(WinRegister[temp].x + i, WinRegister[temp].y + WinRegister[Index].Height - 1 , BackGroundColor);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[temp].x , WinRegister[temp].y + i , BackGroundColor);
		   		vesa_draw_point(WinRegister[temp].x + WinRegister[Index].Width - 1, WinRegister[temp].y + i,BackGroundColor);
			}
	
		break;
		case 2:
			
			temp = WinRegister[Index].Father;
			WinRegister[Index].x=WinRegister[temp].x+7;
			WinRegister[Index].y=WinRegister[temp].y;
			vesa_paint_icon(2,WinRegister[Index].x+1,WinRegister[Index].y+1,BackGroundColor);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , BackGroundColor);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , BackGroundColor);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , BackGroundColor);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, BackGroundColor);
			}
	
		break;
		case 3:
			
			temp = WinRegister[Index].Father;

			WinRegister[Index].x=WinRegister[temp].x+14;
			WinRegister[Index].y=WinRegister[temp].y;

			vesa_paint_icon(3,WinRegister[Index].x+1,WinRegister[Index].y+1,BackGroundColor);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , BackGroundColor);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 ,BackGroundColor);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , BackGroundColor);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, BackGroundColor);
			}
	
		break;
		case 4:

			vesa_draw_rect(0,189,319,199,BackGroundColor);
		break;
		case 5:
			vesa_draw_rect(WinRegister[Index].x,190,WinRegister[Index].x+WinRegister[Index].Width,199,BackGroundColor);
			vesa_paint_string("TASK",WinRegister[Index].x+40,193,BackGroundColor,4);
		break;
		case 6:
			vesa_paint_string(WinRegister[Index].Label,WinRegister[Index].x,WinRegister[Index].y,BackGroundColor,7);
		break;
	}
}

void PaintWin(int Index)
{
	int i, temp;
	switch(WinRegister[Index].Type)
	{
		case 0:	
			
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , WinColorA);
				vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y +6, WinColorA);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , WinColorA);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , WinColorA);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, WinColorA);
			}
			
			for(i = 0; i < NumOfWin; i++) 
				if (WinRegister[i].Father == Index) PaintWin(i);

		break;

		case 1:
			
			temp = WinRegister[Index].Father;
			WinRegister[Index].x=WinRegister[temp].x;
			WinRegister[Index].y=WinRegister[temp].y;
			vesa_paint_letter('X',WinRegister[Index].x+1,WinRegister[Index].y+1,WinColorA);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[temp].x + i, WinRegister[Index].y , WinColorA);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , WinColorA);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , WinColorA);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, WinColorA);
			}
	
		break;
		//Maximize
		case 2:
			
			temp = WinRegister[Index].Father;
			WinRegister[Index].x=WinRegister[temp].x+7;
			WinRegister[Index].y=WinRegister[temp].y;
			vesa_paint_icon(2,WinRegister[Index].x+1,WinRegister[Index].y+1,WinColorA);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , WinColorA);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , WinColorA);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , WinColorA);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, WinColorA);
			}
	
		break;
		case 3:
			
			temp = WinRegister[Index].Father;
			WinRegister[Index].x=WinRegister[temp].x+14;
			WinRegister[Index].y=WinRegister[temp].y;
			vesa_paint_icon(3,WinRegister[Index].x+1,WinRegister[Index].y+1,WinColorA);
			for(i = 0; i < WinRegister[Index].Width; i++)
			{
	 	  		vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , WinColorA);
	  			vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , WinColorA);
			}
	
			for(i = 0; i < WinRegister[Index].Height; i++)
			{
		   		vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , WinColorA);
		   		vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, WinColorA);
			}
	
		break;
		case 4:

			vesa_draw_rect(0,189,319,199,WinColorA);
			vesa_paint_string("GUCAS",3,193,WinColorA,5);
		break;
		case 5:
			vesa_draw_rect(WinRegister[Index].x,190,WinRegister[Index].x+WinRegister[Index].Width,199,WinColorA);
			vesa_paint_string("TASK",WinRegister[Index].x+40,193,WinColorA,4);
		break;
		case 6:
			temp = WinRegister[Index].Father;
			WinRegister[Index].x=WinRegister[temp].x+5;
			WinRegister[Index].y=WinRegister[temp].y+20;
			vesa_paint_string(WinRegister[Index].Label,WinRegister[Index].x,WinRegister[Index].y,WinColorA,strlen(WinRegister[Index].Label));
		break;
	}
}
int getLengthOfStr(char arr[]){
	int i = 0;
	while(1){
		if(arr[i] =='\0') break;
		i++;
	}
	return i;
}
//Write words in windows
/*
void PrintLable(int index, char text[],unsigned int x, unsigned int y,unsigned short color,unsigned int n)
{
	vesa_paint_string(text,WinRegister[Index].x+x,WinRegister[Index].y+y,color,n);

}
*/

void Resize(int Index, int Height, int Width)
{
	int i;
	/*for(i = 0; i < WinRegister[Index].Width; i++)
	{
	   vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y , BackGroundColor);
	   vesa_draw_point(WinRegister[Index].x + i, WinRegister[Index].y + WinRegister[Index].Height - 1 , 0);
	}


	for(i = 0; i < WinRegister[Index].Height; i++)
	{
	   vesa_draw_point(WinRegister[Index].x , WinRegister[Index].y + i , BackGroundColor);
	   vesa_draw_point(WinRegister[Index].x + WinRegister[Index].Width - 1, WinRegister[Index].y + i, 0);
	}*/
	EraseWin(Index);
	WinRegister[Index].Height = Height;
	WinRegister[Index].Width = Width;
	PaintWin(Index);
}
int tempX,tempY,tempHeight,tempWidth;
void Maximize(int Index)
{
	int i=0,j=0;
	
	if(WinRegister[Index].Maximized==0)
	{
		WinRegister[Index].Maximized=1;
		tempX=WinRegister[Index].x;
		tempY=WinRegister[Index].y;
		tempHeight=WinRegister[Index].Height;
		tempWidth=WinRegister[Index].Width;

		EraseWin(Index);
		WinRegister[Index].Height = 190;
		WinRegister[Index].Width = 320;
		WinRegister[Index].x=0;
		WinRegister[Index].y=0;
		for(i = 0; i < NumOfWin; i++) 
		if (WinRegister[i].Type == 0 && i != Index )
		{
			WinRegister[i].Visible=0;
			 EraseWin(i);
			 for(j = 0; j < NumOfWin; j++) 
				if (WinRegister[j].Father == i && WinRegister[j].Type!=5)
				{
					WinRegister[j].Visible=0;
					 EraseWin(j);
				}
		}
		PaintWin(Index);
		
	}
	else
	{	
		EraseWin(Index);
		WinRegister[Index].Height =tempHeight;
		WinRegister[Index].Width = tempWidth;
		WinRegister[Index].x=tempX;
		WinRegister[Index].y=tempY;
		PaintWin(Index);
		WinRegister[Index].Maximized=0;
	}
}
int m_tempX,m_tempY,m_tempHeight,m_tempWidth;
void Minimize(int Index)
{
	int i=0;
	if(WinRegister[Index].Minimized==0)
	{
		WinRegister[Index].Minimized=1;
		m_tempX=WinRegister[Index].x;
		m_tempY=WinRegister[Index].y;
		m_tempHeight=WinRegister[Index].Height;
		m_tempWidth=WinRegister[Index].Width;
	
		WinRegister[Index].Visible=0;
		for(i=0;i<NumOfWin;i++)
		{ 
			if(WinRegister[i].Father==Index && WinRegister[i].Type!=5) WinRegister[i].Visible=0;
		}

		EraseWin(Index);
		
		WinRegister[Index].Height = 0;
		WinRegister[Index].Width = 0;
		WinRegister[Index].x=600;
		WinRegister[Index].y=600;
	}
	else
	{
		EraseWin(Index);
		WinRegister[Index].Height = m_tempHeight;
		WinRegister[Index].Width = m_tempWidth;
		WinRegister[Index].x=m_tempX;
		WinRegister[Index].y=m_tempY;
		WinRegister[Index].Visible=1;
		for(i=0;i<NumOfWin;i++)
		{ 
			if(WinRegister[i].Father==Index && WinRegister[i].Type!=5) WinRegister[i].Visible=1;
		}
		PaintWin(Index);
		WinRegister[Index].Minimized=0;
	}
		
}

void ChangeLabel(int Index, char tempLabel[10] )
{
	EraseWin(Index);
/*
	if(WinRegister[Index].HaveLabel==0)
	{
		WinRegister[Index].HaveLabel=1;
		strcpy(tempLabel,WinRegister[Index].Label);
		strcpy(WinRegister[Index].Label,"JISHENG");
	}
	else 
	{
		WinRegister[Index].HaveLabel=0;
		strcpy(WinRegister[Index].Label,"WINDOW0");
		
	}
*/
	strcpy(WinRegister[Index].Label,"");
	strcpy(WinRegister[Index].Label,tempLabel);
	PaintWin(Index);
}


